package com.example.netty.service;

import com.alibaba.fastjson.JSON;
import com.example.netty.common.Message;
import com.example.netty.constant.ResMessageType;
import com.example.netty.vo.Room;
import com.example.netty.vo.Shoe;
import com.example.netty.vo.User;
import io.netty.channel.Channel;
import io.netty.util.concurrent.ScheduledFuture;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Component;

import java.awt.*;
import java.awt.geom.AffineTransform;
import java.awt.geom.Area;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.Map;
import java.util.UUID;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.TimeUnit;

/**
 * 房间服务类
 * @author zclever <304078606@qq.com>
 * @date 2021-03-02
 */
@Component
public class FightService {

    private static final Logger logger = LoggerFactory.getLogger(FightService.class);

    @Autowired
    private GameService gameService;

    @Autowired
    private RoomService roomService;

    private ScheduledFuture<?> future;

    private int timeCount;

    /**
     * 开始战斗
     * @param channel
     * @param content
     * @return
     */
    public Message startFight(Channel channel, Object content) {
        logger.info("处理开始战斗");

        //获取用户
        User user = gameService.getUserByChannel(channel);

        //获取用户所在房间
        Room room = roomService.getRoomByUser(user);

        //返回消息
        Message message = new Message(ResMessageType.startFight);

        //通知房间内其他用户开始战斗
        for(User u : room.getUserList()){
            if(user==u||u.isRoot())continue;
            Channel ch = gameService.getChannelByUser(u);
            ch.writeAndFlush(message.toMsg());
        }

        //房间倒计时
        logger.info("房间倒计时");
        future = channel.eventLoop().scheduleAtFixedRate(()->{
            timer(gameService,room);
        }, 0, 1, TimeUnit.SECONDS);

        logger.info("返回开始战斗");
        return message;
    }

    /**
     * 房间倒计时
     * @param room
     */
    public void timer(GameService gameService,Room room) {

        if(room.getTime()>0){
            logger.info("房间剩余时间{}秒",room.getTime());

            // 随机改变风向
            double j = Math.random()*100;
            if(j<10){
                int wind = (int)Math.ceil(Math.random()*10) - 5;
                logger.info("改变风向:{}",wind);
                Map map = new HashMap();
                map.put("wind",wind);
                room.setWind(wind);
                Message message = new Message(ResMessageType.wind,map);
                for(User u : room.getUserList()){
                    if(u.isRoot())continue;
                    Channel ch = gameService.getChannelByUser(u);
                    ch.writeAndFlush(message.toMsg());
                }
            }else{
                logger.info("不改变风向");
            }
            room.setTime(room.getTime()-1);

        }else{
            logger.info("房间时间到");
            Message message = new Message(ResMessageType.endFight);
            for(User u : room.getUserList()){
                if(u.isRoot())continue;
                Channel ch = gameService.getChannelByUser(u);
                ch.writeAndFlush(message.toMsg());
            }
            future.cancel(false);
        }
    }

    /**
     * 攻击
     * @param channel
     * @return
     */
    public Message attack(Channel channel, Object content) {
        logger.info("处理攻击");

        //获取用户
        User user = gameService.getUserByChannel(channel);

        //获取用户所在房间
        Room room = roomService.getRoomByUser(user);

        //通知房间内其他用户我攻击
        Message message = new Message(ResMessageType.otherAttack,content);
        for(User u : room.getUserList()){
            if(user==u||u.isRoot())continue;
            Channel ch = gameService.getChannelByUser(u);
            ch.writeAndFlush(message.toMsg());
        }

        logger.info("返回攻击");
        return new Message(ResMessageType.attack);
    }
}
